import time
from datetime import datetime
from datetime import timedelta # 时间运算


# 时间戳time.time()  # 单位是秒, 精度是到微秒
# time获取当前时间time.localtime() # 精度到妙
a = time.localtime()
# 时间格式转换
# `time.struct_time` -> `str`       # time.localtime()可省略
b = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
#`str` -> `time.struct_time`
c = time.strptime("2020-11-06 11:11:11","%Y-%m-%d %H:%M:%S")
#`time.struct_time` -> `tuple`
d = tuple(time.localtime())
#`tuple` -> `time.struct_time`
e = time.struct_time(tuple(d))
#`time.struct_time` -> `timestamp（时间戳）`
f = time.localtime(time.time())
# 没有时区
g = time.gmtime(time.time())
#`timestamp`  -> `time.struct_time`#时间精度的丢失
h = time.mktime(time.localtime())

# datetime查看时间
i = datetime.today()
j = datetime.now(tz=None) # 可以指定时区
k = datetime.utcnow()
# 时间格式的转换
#`datetime.datetime` -> `str`
l = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
#`str` -> `datetime.datetime` # 终端输出和print不一样,调用的2个不同的接口
m = datetime.strptime(l,"%Y-%m-%d %H:%M:%S")
#`datetime.datetime` -> `timetuple` -> `tuple`
n_1 = datetime.now().timetuple()
n = tuple(n_1)
#`tuple` -> `datetime.datetime`
new_n = n[:-3]
o = datetime(*new_n)
#`datetime.datetime` -> `timestamp`
p = datetime.now().timestamp()
#`timestamp` -> `datetime.datetime`
q = datetime.fromtimestamp(p)
# 时间运算
r = datetime.now() + timedelta(hours=1)


print('a',a)
print('b',b)
print('c',c)
print('d',d)
print('e',e)
print('f',f)
print('g',g)
print('h',h)
print('到datetime模块了：')
print('i',i)
print('j',j)
print('k',k)
print('l',l)
print('m',m)
print('n',n)
print('new_n',new_n)
print('o',o)
print('p',p)
print('q',q)
print('r',r)


'''
time

获取当前时间：time.localtime()

改变时区：
	os.environ['TZ'] = 'US'
	time.tzset()

时间戳：time.time()

时间格式转换：
time.struct_time -> str
	time.strftime("%Y-%m-%d %H:%M:%S",time.struct_time)

str -> time.struct_time
	time.sturftime("2020-11-06 10:00:00","%Y-%m-%d %H:%M:%S")

time.struct_time -> tuple
	tuple(time.struct_time)

tuple -> time.struct_time
	time.struct_time(tuple)

time.struct_time -> timestamp
	time.localtime(timestamp) # 提供了有时区接口
	time.gmtime(timestamp) # 没有时区

timestamp -> time.struct_time
	time.mktime(time.struct_time) # 时间精度的丢失

时间运算：
只能通过时间格式->元组, 然后创建新元组的方式来新建一个新的时间

时间休眠：time.sleep()


datetime

使用datetime.datetime模块:
from datetime import datetime

查看时间：
datetime.today()
datetime.now(tz=None) # 可以指定时区
datetime.utcnow()

时间格式的转换：
datetime.datetime -> str
	[datetime.datetime].strftime("%Y-%m-%d %H:%M:%S")

str -> datetime.datetime
	datetime.strptime(str,"%Y-%m-%d %H:%M:%S")

datetime.datetime -> timetuple -> tuple
	tt = [datetime.datetime].timetuple()
	tuple(tt)

tuple -> datetime.datetime
	t = (2020,11,5,22,14,17,3,310,-1)
	new_t = t[:-3]
	datetime(*t)

datetime.datetime -> timestamp
	[datetime.datetime].timestamp()

timestamp -> datetime.datetime
	datetime.fromtimestamp(timestamp)

时间运算：
from fatetime import timedelta


[datetime.datetime] + timedelta(hours=1)
'''
